﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HidesaburoDotNET.Core
{
    public class ScoreCalculator
    {
        static ScoreCalculator calculator;
        public static ScoreCalculator Calculator
        {
            get
            {
                if (calculator == null)
                {
                    calculator = new ScoreCalculator();
                }
                return calculator;
            }
        }

        private ScoreCalculator() { }


        public Scores CalculateScore(Tehai tehai)
        {
            if (tehai.Agari == null || tehai.Agari.Count == 0) return new Scores();
            int fan =0;
            foreach (Yaku y in tehai.Agari)
            {
                fan += y.Fan;
            }
            
            int fu = 0;
            
            int childscore =  CalcAsChild(fan, fu);
            int parentscore = CalcAsParent(fan, fu);
            
            Scores scores = new Scores();
            scores.ChildRon = childscore;
            scores.ParentRon = parentscore;
            scores.ChildTsumoForChild = RoundOut(childscore/4);
            scores.ChildTsumoForParent = RoundOut(childscore/2);
            scores.ParentTsumo = RoundOut(parentscore/3);

            return scores;
        }
        private int CalculateFu(AgariTehai tehai)
        {
            int fu = 20;
            foreach (Mentsu m in tehai.Mentsu)
            {
                fu = fu + m.Fu; 
            }
            
            if(tehai.Tsumo)
            {
                fu = fu + 2;
            }

            if (tehai.AgariMentsu.Type == MentsuType.Syuntsu_A || tehai.AgariMentsu.Type == MentsuType.Syuntsu_M)
            {

                if (tehai.AgariHai == tehai.AgariMentsu[1])
                {
                    fu = fu + 2;
                }
                else if (tehai.AgariMentsu[0].Number == 1 || tehai.AgariMentsu[2].Number == 9)
                {
                    if (tehai.AgariHai.Number == 3 || tehai.AgariHai.Number == 7)
                    {
                        fu = fu + 2;
                    }
                }

            }else if (tehai.AgariMentsu.Type == MentsuType.Toitsu)
            {
                fu = fu + 2;
            }

            return fu;    

        }

        private int RoundOut(int score)
        {
            int point;
            if (score % 100 > 0)
            {

                point = (score / 100 + 1) * 100;
            }
            else
            {
                return score;
            }
            return point;
         }
        
        private int CalcAsChild(int fan, int fu)
        {
            int score;
            score = 16 * fu * (int)Math.Pow(2, fan);
            return RoundOut(score);
        }

        private int CalcAsParent(int fan, int fu)
        {
            int score;
            score = 24 * fu * (int)Math.Pow(2, fan);
            return RoundOut(score);
        }

        
    }
    public struct Scores
    {
        int childRon;

        public int ChildRon
        {
            get { return childRon; }
            set { childRon = value; }
        }
        int childTsumoForParent;

        public int ChildTsumoForParent
        {
            get { return childTsumoForParent; }
            set { childTsumoForParent = value; }
        }
        int childTsumoForChild;

        public int ChildTsumoForChild
        {
            get { return childTsumoForChild; }
            set { childTsumoForChild = value; }
        }
        int parentRon;

        public int ParentRon
        {
            get { return parentRon; }
            set { parentRon = value; }
        }
        int parentTsumo;

        public int ParentTsumo
        {
            get { return parentTsumo; }
            set { parentTsumo = value; }
        }
    }
}
